{% extends "base.html" %}

{% block title %}抽题系统 - 管理后台{% endblock %}

{% block styles %}
    <link rel="stylesheet" href="{{ url_for('static', filename='css/admin.css') }}">
{% endblock %}

{% block navigation %}
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container-fluid">
        <a class="navbar-brand" href="{{ url_for('admin.dashboard') }}">抽题系统管理后台</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('admin.dashboard') }}">控制面板</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('admin.draw_records') }}">抽题记录</a>
                </li>
                {% if current_user.is_super_admin() %}
                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('admin.questions') }}">题库管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('admin.config') }}">系统配置</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('admin.invite_codes') }}">邀请码管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('admin.statistics') }}">抽题统计</a>
                </li>
                {% endif %}
            </ul>
            <ul class="navbar-nav ms-auto">
                <li class="nav-item">
                    <span class="nav-link">欢迎，{{ current_user.username }}</span>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ url_for('admin.logout') }}">退出</a>
                </li>
            </ul>
        </div>
    </div>
</nav>
{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row">
        <!-- 侧边栏 -->
        <div class="col-lg-3 mb-4">
            <div class="card">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">管理菜单</h6>
                </div>
                <div class="card-body">
                    <div class="list-group list-group-flush">
                        <a href="{{ url_for('admin.dashboard') }}" class="list-group-item list-group-item-action">
                            <i class="bi bi-speedometer2 me-2"></i>控制面板
                        </a>
                        {% if current_user.is_super_admin() %}
                        <a href="{{ url_for('admin.questions') }}" class="list-group-item list-group-item-action">
                            <i class="bi bi-question-circle me-2"></i>题目管理
                        </a>
                        <a href="{{ url_for('admin.config') }}" class="list-group-item list-group-item-action">
                            <i class="bi bi-gear me-2"></i>系统配置
                        </a>
                        <a href="{{ url_for('admin.invite_codes') }}" class="list-group-item list-group-item-action">
                            <i class="bi bi-key me-2"></i>邀请码管理
                        </a>
                        <a href="{{ url_for('admin.statistics') }}" class="list-group-item list-group-item-action">
                            <i class="bi bi-bar-chart me-2"></i>抽题统计
                        </a>
                        {% endif %}
                        <a href="{{ url_for('admin.draw_records') }}" class="list-group-item list-group-item-action">
                            <i class="bi bi-list-check me-2"></i>抽题记录
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 主内容 -->
        <div class="col-lg-9">
            {% block admin_content %}{% endblock %}
        </div>
    </div>
</div>

<!-- 权限不足模态框 -->
<div class="modal fade" id="permissionDeniedModal" tabindex="-1" aria-labelledby="permissionDeniedModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="permissionDeniedModalLabel">权限不足</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                您没有权限执行此操作。
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
// 全局 AJAX 错误处理
$(document).ajaxError(function(event, jqXHR, settings, error) {
    if (jqXHR.status === 403) {
        const response = JSON.parse(jqXHR.responseText);
        if (response.error === '权限不足') {
            const modal = new bootstrap.Modal(document.getElementById('permissionDeniedModal'));
            modal.show();
        }
    }
});
</script>
{% endblock %} 